// @(#)log.cc
// Time-stamp: <2008-12-31 18:12:15 lancer>
// Copyright 2008 Julian Qian
// Author: root@localhost.localdomain
// Version: $Id: log.cc,v 0.0 2008/11/20 06:14:05 root Exp $
// Keywords: 

#include "common.hh"
#include "log.hh"

#include <log4cxx/logmanager.h>
#include <log4cxx/xml/domconfigurator.h>
#include <log4cxx/patternlayout.h>
#include <log4cxx/rolling/rollingfileappender.h>
#include <log4cxx/rolling/fixedwindowrollingpolicy.h>
#include <log4cxx/rolling/filterbasedtriggeringpolicy.h>
#include <log4cxx/filter/levelrangefilter.h>
#include <log4cxx/helpers/pool.h>
// #include <log4cxx/logger.h>
#include <log4cxx/propertyconfigurator.h>
#include <log4cxx/dailyrollingfileappender.h>
#include <log4cxx/helpers/stringhelper.h>
#include <log4cxx/stream.h>


using namespace log4cxx::rolling;
using namespace log4cxx::xml;
using namespace log4cxx::filter;
using namespace log4cxx::helpers;

Log* Log::_theLog = NULL;

LoggerPtr
Log::theLog(){
    
    if(NULL == _theLog){
        _theLog = new Log;
    }
    
    LoggerPtr logger(Logger::getLogger("org.apache.log4j.DailyRollingFileAppender"));
    return logger;
}

void
Log::endLog(){
    delete _theLog;
}

Log::Log(){
    PropertyConfigurator::configure(File(LOG_CONF_FILE));
}

Log::~Log(){
    
}
// void
// Log::log_debug(std::ostream& os){
//     LOG4CXX_DEBUG(logger, os);
// }

int
Log::getFileCount(const char* dir, const LogString& initial) {
    Pool p;
    std::vector<LogString> files(File(dir).list(p));
    int count = 0;

    for (size_t i = 0; i < files.size(); i++) {
        if (StringHelper::startsWith(files[i], initial)) {
            count++;
        }
    }

    return count;
}


void
Log::tearDown() {
    LogManager::shutdown();
}

